from Tkinter import *
import Tkinter
import os
import stations
import weather
import tweets
import news
import time


#turn on audio
os.system("sudo modprobe snd_bcm2835")

#retrieve stations list and categories
stations,categories = stations.station_list()

#retrieve_weather
weather = weather.retrieve_weather()

#define font
font_style = ("Helvetica",26,"bold")

#define button style
button_width = 300
button_height = 1
text_color = "black"
active_text_color = "white"
bg_color = "white"
active_bg_color = "black"


def dict_to_list(temp_dict):
    temp_list = []
    for i in temp_dict:
        temp_list.append(i)

    return temp_list


def station_menu(current_category):
    
    station_window=Tkinter.Toplevel()
       
    w_s, h_s = root.winfo_screenwidth(), root.winfo_screenheight()
    station_window.overrideredirect(1)
    station_window.geometry("%dx%d+0+0" % (w_s, h_s))
    station_window.configure(background = bg_color)

    station_window.focus_set() # <-- move focus to this widget
    station_window.bind("<Escape>", lambda e: e.widget.quit())

    g=Frame(station_window)
    g.pack()

    station_list = dict_to_list (stations[current_category])    

    for indx, option in enumerate(station_list):
        cmd = lambda opt=option:play(opt,current_category)
        sta_btn = Tkinter.Button(station_window,text=option,font = font_style,highlightthickness = 0, fg = text_color,width = button_width,height = button_height, bg = bg_color, activebackground = active_bg_color, activeforeground = active_text_color,relief=FLAT, command=cmd)
        sta_btn.pack(side='top')

    Tkinter.Button(station_window,font = font_style,highlightthickness = 0, fg = text_color,width = button_width,height = button_height,  bg = bg_color, activebackground = active_bg_color, activeforeground = active_text_color,  text="Go Back", relief=FLAT, command=station_window.destroy).pack(side=Tkinter.TOP)


def category_menu():

    category_window=Tkinter.Toplevel()

    w_c, h_c = root.winfo_screenwidth(), root.winfo_screenheight()
    category_window.overrideredirect(1)
    category_window.geometry("%dx%d+0+0" % (w_c, h_c))
    category_window.configure(background = bg_color)

    category_window.focus_set() # <-- move focus to this widget
    category_window.bind("<Escape>", lambda e: e.widget.quit())

    j=Frame(category_window)
    j.pack()

    
    for indx, option in enumerate(categories):
        cmd = lambda opt=option:station_menu(opt)
        cat_btn = Tkinter.Button(category_window,text=option,highlightthickness = 0, font = font_style, fg = text_color,width = button_width,height = button_height, bg = bg_color, activebackground = active_bg_color, activeforeground = active_text_color,relief=FLAT, command=cmd)
        cat_btn.pack(side='top')

    Tkinter.Button(category_window,font = font_style,highlightthickness = 0, fg = text_color,width = button_width,height = button_height,  bg = bg_color, activebackground = active_bg_color, activeforeground = active_text_color,  text="Go Back", relief=FLAT, command=category_window.destroy).pack(side=Tkinter.TOP)


def show_weather():

    weather_window=Tkinter.Toplevel()

    w_w, h_w = root.winfo_screenwidth(), root.winfo_screenheight()
    weather_window.overrideredirect(1)
    weather_window.geometry("%dx%d+0+0" % (w_w, h_w))
    weather_window.configure(background = bg_color)

    b=Frame(weather_window)
    b.pack()

    current_temp = weather['condition']['temp']
    current_conditions = weather['condition']['text']

    today_high =  weather['forecasts'][0]['high']
    today_low =  weather['forecasts'][0]['low']
    today_conditions = weather['forecasts'][0]['text']

    tomorrow_high = weather['forecasts'][1]['high']
    tomorrow_low =  weather['forecasts'][1]['low']
    tomorrow_conditions = weather['forecasts'][1]['text']

    display_weather_current = "Now: \n" + current_temp + "F\n" + current_conditions + "\n\n"
    display_weather_today = "Today: \nHigh " + today_high + "F \nLow " + today_low + "F \n" + today_conditions + "\n\n"
    display_weather_tomorrow = "Tomorrow: \nHigh " + tomorrow_high + "F \nLow " + tomorrow_low + "F \n" + tomorrow_conditions
 
    Tkinter.Button(weather_window,font = font_style,highlightthickness = 0, fg = text_color,width = button_width,height = button_height,  bg = bg_color, activebackground = active_bg_color, activeforeground = active_text_color,  text="Go Back", relief=FLAT, command=weather_window.destroy).pack(side=Tkinter.BOTTOM)

    text=Text(weather_window,font=font_style)
    text.pack()
    text.insert(END, display_weather_current)
    text.insert(END, display_weather_today)
    text.insert(END, display_weather_tomorrow)

def show_twitter():


    twitter_window=Tkinter.Toplevel()

    w_t, h_t = root.winfo_screenwidth(), root.winfo_screenheight()
    twitter_window.overrideredirect(1)
    twitter_window.geometry("%dx%d+0+0" % (w_t, h_t))
    twitter_window.configure(background = bg_color)

    t=Frame(twitter_window)
    t.pack()

    #exit button    
    Tkinter.Button(twitter_window,font = font_style,highlightthickness = 0, fg = text_color,width = button_width,height = button_height,  bg = bg_color, activebackground = active_bg_color, activeforeground = active_text_color,  text="Go Back", relief=FLAT, command=twitter_window.destroy).pack(side=Tkinter.BOTTOM)

    text=Text(twitter_window,font=font_style)

    text.pack()

    tweet = tweets.twitter()

    for i in range(0,len(tweet)):
       text.insert(END, tweet[i])



def show_news():

    news_window=Tkinter.Toplevel()

    w_n, h_n= root.winfo_screenwidth(), root.winfo_screenheight()
    news_window.overrideredirect(1)
    news_window.geometry("%dx%d+0+0" % (w_n, h_n))
    news_window.configure(background = bg_color)

    t=Frame(news_window)
    t.pack()

    #exit button    
    Tkinter.Button(news_window,font = font_style,highlightthickness = 0, fg = text_color,width = button_width,height = button_height,  bg = bg_color, activebackground = active_bg_color, activeforeground = active_text_color,  text="Go Back", relief=FLAT, command=news_window.destroy).pack(side=Tkinter.BOTTOM)

    text=Text(news_window,font=font_style)

    text.pack()

    articles = news.news()

    for i in range(0,len(articles)):
       text.insert(END, articles[i])

              

def play(current_station,current_category):
    
    play_category = stations[current_category]
    
    play_station = play_category[current_station]

    os.system("sudo mpc clear")
    os.system("sudo mpc add " + str(play_station))
    os.system("sudo mpc play")

      



root = Tkinter.Tk()

first_btn = Tkinter.Button(text="Radio",highlightthickness = 0, font = font_style, fg = text_color,width = button_width,height = button_height,bg = bg_color, activebackground = active_bg_color, activeforeground = active_text_color,relief=FLAT, command=category_menu)
second_btn = Tkinter.Button(text="Weather",highlightthickness = 0, font = font_style, fg = text_color,width = button_width,height = button_height,bg = bg_color, activebackground = active_bg_color, activeforeground = active_text_color,relief=FLAT, command=show_weather)
third_btn = Tkinter.Button(text="Twitter",highlightthickness = 0, font = font_style, fg = text_color,width = button_width,height = button_height,bg = bg_color, activebackground = active_bg_color, activeforeground = active_text_color,relief=FLAT, command=show_twitter)
fourth_btn = Tkinter.Button(text="News",highlightthickness = 0, font = font_style, fg = text_color,width = button_width,height = button_height,bg = bg_color, activebackground = active_bg_color, activeforeground = active_text_color,relief=FLAT, command=show_news)
first_btn.pack(side='top')
second_btn.pack(side='top')
third_btn.pack(side='top')
fourth_btn.pack(side='top')


# make it cover the entire screen
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(1)
root.geometry("%dx%d+0+0" % (w, h))
root.configure(background = bg_color)


# <-- move focus to this widget

f=Frame(root)
f.focus_set()

"""f.bind_all('<c>', callback)"""

f.pack()

root.mainloop()



